<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends Member {
    private $_user = 'user';
	 public function __construct()
    {
        parent::__construct();
		$this->load->model('captcha_model');
		$this->load->model(array('user_model','post_model'));

    }
	public function index()
	{
		$data = array();
        $user_id = $this->userdata['id'];
        $this->form_validation->set_rules('email', 'Nhập địa chỉ email.', 'required');
        $this->form_validation->set_rules('mobile', 'Nhập số điện thoại', 'required');
        if($this->form_validation->run()==true){
            $obj=array();
            $obj['fullname']    =   $this->input->post('fullname');
            $obj['mobile']      =   $this->input->post('mobile');
            $obj['hide_email']  =   $this->input->post('hide_email');
            $obj['modifyTime']  =   time();
            $obj['phone']       =   $this->input->post('home_phone');
            $obj['fax']         =   $this->input->post('fax');
            $obj['address']     =   $this->input->post('address');
            $obj['birthday']    =   $this->input->post('birthday');
            $obj['cmt']         =   $this->input->post('cmt');
            $obj['hide_phone']  =   $this->input->post('show_home_phone');
            $obj['yahoo']       =   $this->input->post('yahoo');
            $obj['skype']       =   $this->input->post('skype');
            $obj['is_alert']    =   $this->input->post('email_alert');
            $obj['website']     =   $this->input->post('website');
            if($this->input->post('avatar')){
                $obj['avatar']     =   $this->input->post('avatar');
            }

            $obj['contact']     =   $this->input->post('contact');
            $this->user_model->update($obj,$user_id);
        }
        $data['member'] = $this->user_model->getSingle($user_id);
        $data['category_id']=0;
		$this->layout->view($data);
	}

    public function changepassword(){
        $data=array();
        $this->form_validation->set_rules('old_password', 'Chưa nhận mật khẩu cũ.', 'required|callback_is_unique_password');
        $this->form_validation->set_rules('new_password', 'Mật khẩu', 'required|min_length[6]');
        $this->form_validation->set_rules('retype_new_password', 'Nhắc lại mật khẩu', 'required|matches[new_password]');
        if($this->form_validation->run()==true){
            $obj=array();
            $obj['password']=MD5($this->input->post('new_password'));
            $user_id = $this->userdata['id'];
            $this->db->where('id',$user_id);
            $this->db->update($this->_user,$obj);
            $data['success']="Cập nhật thành công";
        }
        $data['category_id']=0;
        $this->layout->view($data);
    }

    public function is_unique_password($password){
        if(md5($password)!=$this->userdata['password']){
            $this->form_validation->set_message ( 'is_unique_password', 'Mật khẩu cũ không đúng.' );
            return false;
        }
        return true;
    }

    public function post(){
        $data=array();
        $user_id=$this->userdata['id'];
        $page = $this->uri->segment(4);
        if(empty($page)){
            $page=1;
        }
        $page_size = 5;
        $page_url   = base_url('usercp/user/post');
        $total_rows = $this->post_model->member_count_all($user_id);
        $records = $this->post_model->member_find_all($user_id,$page, $page_size);
        $pagination = $this->get_paging($page_url, $total_rows, $page, $page_size );
        $data = array(
            "total" => $total_rows,
            "records" => $records,
            "pagination" => $pagination
        );
        $data['category_id']=0;
        if($this->input->is_ajax_request()){
            $this->load->view("member/user/ajax_post",$data);
        }else{
            $this->layout->view($data);
        }
    }


	//validate captcha
	function is_valid_captcha($str) {
		if($this->captcha_model->captcha_check($str)){
			return TRUE;
		}else{
			$this->form_validation->set_message ( 'is_valid_captcha', '%s không chính xác' );
			return FALSE;
		}

	}
	//validate email
	function is_unique_email($email){
		$user = $this->user_model->getByEmail( $email);
		if($user!=null){
			$this->form_validation->set_message('is_unique_email', 'Địa chỉ email đã có người đăng ký');
			return false;
		}
		return true;
	}

	function is_unique_username($username){
		$user = $this->user_model->getByUserName( $username);
		if($user!=null){
			$this->form_validation->set_message('is_unique_username', 'Địa chỉ email đã có người đăng ký');
			return false;
		}
		return true;
	}

}